package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookAuthorMapper;
import com.kgc.novel.domain.entity.BookAuthor;
import com.kgc.novel.domain.param.BookAuthorParam;
import com.kgc.novel.service.book.BookAuthorService;
import com.kgc.novel.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author mac
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BookAuthorServiceImpl extends ServiceImpl<BookAuthorMapper, BookAuthor> implements BookAuthorService {

    @Resource
    private BookAuthorMapper bookAuthorMapper;

    @Override
    public Res selectAuthorAll(BookAuthorParam bookAuthorParam) {
        Objects.nonNull(bookAuthorParam);
        Page<BookAuthor> page = new Page<>(bookAuthorParam.getPageNo(), bookAuthorParam.getPageSize());
        LambdaQueryWrapper<BookAuthor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(bookAuthorParam.getName()),BookAuthor::getName,bookAuthorParam.getName());
        Page<BookAuthor> bookAuthorPage = bookAuthorMapper.selectPage(page, lambdaQueryWrapper);
        return Res.returnData(bookAuthorPage,bookAuthorPage.getTotal());
    }

    @Override
    public Res selectAuthorNoPage() {
        QueryWrapper<BookAuthor> queryWrapper = new QueryWrapper<>();
        List<BookAuthor> list = bookAuthorMapper.selectList(queryWrapper);
        return Res.returnData(list);
    }

    @Override
    public Res getSingleAuthor(String id) {
        BookAuthor bookAuthor = bookAuthorMapper.selectById(id);
        if (bookAuthor != null) {
            return Res.returnData("200",bookAuthor);
        }
        return Res.returnMsg("暂无该作者的信息！");
    }

    @Override
    public Res addAuthor(BookAuthor bookAuthor) {
        LambdaQueryWrapper<BookAuthor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookAuthor::getName,bookAuthor.getName());

        BookAuthor bookAuthorObj = bookAuthorMapper.selectOne(lambdaQueryWrapper);
        if (bookAuthorObj != null){
            return Res.returnErrMsg("该笔名已存在，请重新添加！");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        bookAuthor.setId(String.valueOf(id));
        bookAuthorMapper.insert(bookAuthor);
        return Res.returnMsg("添加成功！");
    }

    @Override
    public Res updateAuthor(BookAuthor bookAuthor) {
        LambdaQueryWrapper<BookAuthor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookAuthor::getName,bookAuthor.getName());

        BookAuthor bookAuthorObj = bookAuthorMapper .selectOne(lambdaQueryWrapper);
        if (bookAuthorObj == null){
            bookAuthorMapper.updateById(bookAuthor);
            return Res.returnMsg("修改成功！");
        }
        BookAuthor oldBookAuthor = bookAuthorMapper.selectById(bookAuthor.getId());
        if (oldBookAuthor.getName().equals(bookAuthor.getName())&&oldBookAuthor.getId().equals(bookAuthor.getId())){
            bookAuthorMapper.updateById(bookAuthor);
            return Res.returnMsg("修改成功！");
        }
        return Res.returnErrMsg("该笔名已存在，请重新修改！");
    }

    @Override
    public Res deleteAuthor(String id) {
        int i = bookAuthorMapper.deleteById(id);
        if (i != -1){
            return Res.returnMsg("删除成功！");
        }else {
            return Res.returnMsg("删除失败！");
        }
    }

    @Override
    public Res deleteAllAuthor(List<String> ids) {
        int i = bookAuthorMapper.deleteBatchIds(ids);
        if (i != -1){
            return Res.returnMsg("批量删除成功");
        }else {
            return Res.returnMsg("批量删除失败");
        }
    }


}
